package lc;
import java.util.*;
import org.junit.*;
public class Ex166 {
    class Solution {
        public String fractionToDecimal(int _a, int _b) {
            boolean flag = true;
            if (_a < 0 && _b > 0 || _a > 0 && _b < 0) {
                flag = false;
            }
            long a = Math.abs(_a);
            long b = Math.abs((long)_b);
            StringBuilder sb = new StringBuilder();
            sb.append(flag ? "" : "-");
            sb.append(a / b);
            if (a % b == 0) {
                return sb.toString();
            }  
            sb.append(".");
            a %= b;
            Map<Long, Integer> map = new HashMap<>();
            long t = a;
            int i = sb.length();
            while (t != 0) {
                if (map.containsKey(t)) {
                    int start = map.get(t);
                    sb.insert(start, "(");
                    sb.append(")");
                    break;
                }
                map.put(t, i);
                t *= 10;
                while (t < b) {
                    t *= 10;
                    sb.append("0");
                    i++;
                }
                sb.append(t / b);
                t %= b;
                i++;
            }
            return sb.toString();
        }
    }

    @Test
    public void test() {
        Solution s = new Solution();
        System.out.println(s.fractionToDecimal(-1, -2147483648));        
    }
}
